package as3.component.controls
{
	import as3.component.Component;
	import as3.component.event.SizeChangeEvent;
	import as3.component.geom.IntRectangle;
	import as3.component.support.CellRenderer;
	import as3.component.support.DataProvider;
	import as3.component.support.ListData;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class List extends Component
	{
		public var scrollPane:ScrollPane;
		
		protected var _dataProvide:DataProvider;
		
		private var _itemList:Array = [];
		
		private var cellHeight:Number = 0;
		public var cellWidth:Number = 0;
		
		
		private var _selectItem:Object;
		private var _selectRender:CellRenderer;
		
		private var _rendererClass:Class;
		
		public function List()
		{
			
		}
		
		override protected function initCom():void
		{
			setMasked(true);
			scrollPane = new ScrollPane();
			scrollPane.verticalScrollPolicy = ScrollBarPolicy.ON;
			addChild(scrollPane);
			scrollPane.addEventListener(SizeChangeEvent.SIZE_CHANGE, onSzieChange);
			
			var cell:CellRenderer = new CellRenderer();
			cellHeight = cell.defaultHeight;
			
			scrollPane.pageScrollSize = cellHeight;
			_rendererClass = CellRenderer;
			
		}
		
		private function onSzieChange(evt:SizeChangeEvent):void
		{
			if (cellWidth != scrollPane.continerScrollRect.width) 
			{
				cellWidth = scrollPane.continerScrollRect.width;
				if (_itemList.length > 0) 
				{
					var len:int = _itemList.length;
					for (var i:int = 0; i < len; i++) 
					{
						_itemList[i].setSize(cellWidth, cellHeight);
					}
				}
			}
		}
		
		public function setCellRendererClass(value:Class):void
		{
			_rendererClass = value;
		}
		
		public function getCellHeight():Number
		{
			return cellHeight;
		}
		
		public function get selectItem():Object
		{
			return _selectItem;
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			scrollPane.width = width;
			setMaskRect(new IntRectangle(0,0,width, height));
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			scrollPane.height = height;
			setMaskRect(new IntRectangle(0,0,width, height));
		}
		
		public function set dataProvide(value:DataProvider):void
		{
			_dataProvide = value;
			updateItemList();
		}
		
		public function get dataProvide():DataProvider
		{
			return _dataProvide;
		}
		
		public function set verticalScrollPolicy(value:String):void
		{
			scrollPane.verticalScrollPolicy = value;
		}
		
		public function get verticalScrollPolicy():String
		{
			return scrollPane.verticalScrollPolicy;
		}
		
		protected function updateItemList():void
		{
			var i:int = 0;
			var item:CellRenderer;
			if (dataProvide) 
			{
				var need:int = _itemList.length - dataProvide.length;
				if (need < 0) 
				{
					need = Math.abs(need);
					for (i = 0; i < need; i++)
					{
						item = new _rendererClass();
						item.setSize(cellWidth, item.defaultHeight);
						item.doubleClickEnabled = true;
						item.addEventListener(MouseEvent.CLICK, onCellClick);
						_itemList.push(item);
					}
				} 
				else if (need > 0) 
				{
					var temp:Array = _itemList.splice(0, need);
					for (i = 0; i < temp.length; i++) 
					{
						item = temp[i];
						scrollPane.removeChild(item);
						item.data = null;
						item.listData = null;
						item = null;
					}
				}
				var data:Object;
				var hasSelect:Boolean = false;
				for (i = 0; i < _itemList.length; i++)
				{
					item = _itemList[i];
					data = dataProvide.getItemAt(i);
					var listData:ListData = new ListData(data.label, data.icon, i, i);
					item.data = data;
					item.listData = listData;
					item.y = item.defaultHeight * i;
					if(data.color){
						item._textField.color = data.color;
						item._upTextColor = data.color;
					}
					if (data == _selectItem) 
					{
						hasSelect = true;
						if (_selectRender) 
						{
							_selectRender.selected = false;
						}
						_selectRender = item;
						_selectRender.selected = true;
						_selectItem = _selectRender.data;
					}
					if (!scrollPane.contains(item)) 
					{
						scrollPane.addChild(item);
					}
				}
				if (!hasSelect && _selectItem) 
				{
					_selectItem = null;
					_selectRender = null;
				}
			} 
			else 
			{
				for (i = 0; i < _itemList.length; i++) 
				{
					item = _itemList[i];
					item.data = null;
					item.listData = null;
					scrollPane.removeChild(item);
					item = null;
				}
			}
			width=scrollPane.width;
			height=scrollPane.height;
		}
		
		private function onCellClick(evt:MouseEvent):void
		{
			var item:CellRenderer = evt.currentTarget as CellRenderer;
			if (item.data == _selectItem) return;
			if (_selectRender) {
				_selectRender.selected = false;
			}
			_selectRender = item;
			_selectRender.selected = true;
			_selectItem = _selectRender.data;
			dispatchEvent(new Event(Event.SELECT));
		}
		
		public function get selectIndex():int
		{
			if (dataProvide) return dataProvide.getItemIndex(_selectItem);
			else return -1;
		}
		
		public function set selectIndex(value:int):void
		{
			if (value == selectIndex) return;
			if (value >= _itemList.length || value < 0) return;
			_selectItem = dataProvide.getItemAt(value);
			if (_selectRender) {
				_selectRender.selected = false;
			}
			_selectRender = _itemList[value];
			_selectRender.selected = true;
			dispatchEvent(new Event(Event.SELECT));
		}
		
		public function clearData():void
		{
			var item:CellRenderer;
			for (var i:uint = 0; i < _itemList.length; i++) 
			{
				item = _itemList[i];
				item.data = null;
				item.listData = null;
				scrollPane.removeChild(item);
				_itemList.splice(i,1);
				item = null;
			}
		}
		
		public function get length():int
		{
			if (dataProvide) return dataProvide.length;
			else return 0;
		}
	}
}